package com.app.common.utils;

import com.app.modules.code.domain.Sysdbconfig;
import com.app.modules.code.vo.CodeconfigVo;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;

public class MyBatisPlusGeneratorUtils {

    protected final static Logger logger = LoggerFactory.getLogger(MyBatisPlusGeneratorUtils.class);

    //项目在硬盘上的基础路径
    public static final String PROJECT_PATH = System.getProperty("user.dir");
    //JAVA文件路径
    public static final String JAVA_PATH = "/src/main/java";
    //资源文件路径
    public static final String RESOURCES_PATH = "/src/main/resources";

    //作者
    public static String author= "MyBatisPlus";

    //默认包名
    public static String parent = "com.app.modules.test";

    //Swagger2
    public static boolean isSwagger2 = false;

    //生成的code
    public static String generatorCode(CodeconfigVo codevo, Sysdbconfig sysdbconfig) throws IOException {

        //String FilePuth = PROJECT_PATH + JAVA_PATH;
        String FilePuth = PROJECT_PATH + RESOURCES_PATH + "/code";

        if(codevo.getAuthor() != null || codevo.getAuthor() != "") {
            author = codevo.getAuthor();
        }

        if(codevo.getTableprefix()!=null || codevo.getTableprefix()!="") {
            parent = codevo.getTableprefix();
        }


        //全局配置
        GlobalConfig config = new GlobalConfig();
        //是否支持AR模式
        config.setActiveRecord(true)
                //作者
                .setAuthor(author)
                //生成路径
                .setOutputDir(FilePuth)
                //文件覆盖
                .setFileOverride(true)
                // 主键策略
                .setIdType(IdType.AUTO)
                //设置生成的service接口的名字的首字母是否为I (案列:IEmployeeService)
                .setServiceName("%sService")
                // XML 二级缓存
                //.setEnableCache(false)
                //生成基本的resultMap
                .setBaseResultMap(true)
                // 实体属性 Swagger2 注解
                .setSwagger2(codevo.isSwagger2())
                //生成基本的SQL片段
                .setBaseColumnList(true);

        //数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();

        //设置数据库类型
        if(sysdbconfig.getDbtype().equals("mysql")) {
            dsConfig.setDbType(DbType.MYSQL);
        } else if (sysdbconfig.getDbtype().equals("mariadb")) {
            dsConfig.setDbType(DbType.MARIADB);
        } else if (sysdbconfig.getDbtype().equals("oracle")) {
            dsConfig.setDbType(DbType.ORACLE);
        } else if (sysdbconfig.getDbtype().equals("db2")) {
            dsConfig.setDbType(DbType.DB2);
        } else if (sysdbconfig.getDbtype().equals("h2")) {
            dsConfig.setDbType(DbType.H2);
        } else if (sysdbconfig.getDbtype().equals("hsql")) {
            dsConfig.setDbType(DbType.HSQL);
        } else if (sysdbconfig.getDbtype().equals("sqlite")) {
            dsConfig.setDbType(DbType.SQLITE);
        } else if (sysdbconfig.getDbtype().equals("postgresql")) {
            dsConfig.setDbType(DbType.POSTGRE_SQL);
        } else if (sysdbconfig.getDbtype().equals("sqlserver2005")) {
            dsConfig.setDbType(DbType.SQL_SERVER2005);
        } else if (sysdbconfig.getDbtype().equals("sqlserver")) {
            dsConfig.setDbType(DbType.SQL_SERVER);
        } else if (sysdbconfig.getDbtype().equals("dm")) {
            dsConfig.setDbType(DbType.DM);
        } else if (sysdbconfig.getDbtype().equals("other")) {
            dsConfig.setDbType(DbType.OTHER);
        }

        dsConfig.setDriverName(sysdbconfig.getDriver())
                .setUrl(sysdbconfig.getUrl())
                .setUsername(sysdbconfig.getUsername())
                .setPassword(sysdbconfig.getPassword());

        //策略配置globalConfiguration
        StrategyConfig stConfig = new StrategyConfig();

        //全局大写命名
        stConfig.setCapitalMode(true)
                //指定表名 字段名是否使用下划线
                //.setDbColumnUnderline(true)
                //数据库表映射到实体的命名策略
                .setNaming(NamingStrategy.underline_to_camel)
                //字段映射实体去除下划线
                .setColumnNaming(NamingStrategy.no_change)
                //表前缀
                .setTablePrefix(parent)
                //生成的表
                .setInclude(codevo.getDbtables());

        PackageConfig pkConfig = new PackageConfig();
        pkConfig.setParent(codevo.getPackagename()==null?parent:codevo.getPackagename())
                .setMapper("mapper")//dao
                .setService("service")//servcie
                .setController("controller")//controller
                .setEntity("domain")
                .setXml("mapper");//mapper.xml
        //整合配置
        AutoGenerator ag = new AutoGenerator();
        ag.setGlobalConfig(config)
                .setDataSource(dsConfig)
                .setStrategy(stConfig)
                .setPackageInfo(pkConfig);
        //执行
        ag.execute();

        //生成文件返回路径
         String zipPath = FilePuth + "/" + StringUtils.replace(parent, ".", "/");
        return zipPath;

    }




















}
